package com.jinyu.admin.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import com.jinyu.admin.service.TestService;
import com.jinyu.admin.utils.RedisUtils;
import com.jinyu.common.base.ApiRestResponse;
import com.jinyu.dao.entity.TestEntity;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: yangli
 * @Despriction:    测试模块
 * @Package: com.jinyu.admin.controller
 * @Date:Created in 2022/5/1 23:24
 * @Modify By:
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
    @Resource
     RedisUtils redisUtils;

    @Autowired
    private TestService testService;

    /**
     *  测试接口1.0
     * @return  ApiRestResponse
     */
    @GetMapping("/1.0")
    public ApiRestResponse<Object> test () {

        redisUtils.setCacheObject("15",1);
        return ApiRestResponse.ok();
    }

    /**
     *  测试新增接口
     */
    @PostMapping("/add")
    public ApiRestResponse<Object> addTest (@RequestBody @Valid TestEntity testEntity) {
        log.info("请求参数：{}",testEntity.toString());
        long snowflakeNextId = IdUtil.getSnowflakeNextId();
        testEntity.setId(snowflakeNextId);
        testService.add(testEntity);
        return ApiRestResponse.ok();
    }

    /**
     *  获取一条测试数据
     * @param id    主键ID
     * @return  ApiRestResponse
     */
    @GetMapping("/getTestEntityByOne")
    public ApiRestResponse<TestEntity> getTestEntityByOne (@RequestParam("id") long id) {
        TestEntity testEntity = testService.getTestEntityByOne(id);
        return ApiRestResponse.ok("获取成功",testEntity);
    }

    /**
     *  测试登录
     * @param username  用户名
     * @param password  密码
     * @return  ApiRestResponse
     */
    @GetMapping("/doTestLogin")
    public ApiRestResponse<SaTokenInfo> doTestLogin (@RequestParam("username") String username,@RequestParam("password") String password) {
        if("zhang".equals(username) && "123456".equals(password)) {
            StpUtil.login(10001,"PC");
            SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
            return ApiRestResponse.ok("登录成功",tokenInfo);
        }
        return ApiRestResponse.err("登录失败");
    }

    /**
     *  测试校验登录
     * @return  ApiRestResponse
     */
    @GetMapping("/isLogin")
    public ApiRestResponse<String> isLogin () {
        boolean login = StpUtil.isLogin();
        return login ? ApiRestResponse.ok("当前会话已登陆") : ApiRestResponse.err("当前会话未登陆");
    }

}
